﻿using SanXiao.Game.Common;
using SanXiao.Game.View.ViewEx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SanXiao.Game.GameCenter.Bubbles
{
    public abstract class BubbleObject : IDisposable
    {
        private readonly ImageObject _image;
        public BubbleObject(ImageObject imageObject, BubbleType bubbleType)
        {
            BubbleType = bubbleType;
            _image = imageObject;
        }
        public BubbleType BubbleType { get; private set; }
        public ImageObject Image => _image;
        public string Text { get; set; }
        public Font Font { get; set; }
        public int X { get; set; }
        public int Y { get; set; }
        private string _lastText;
        private SizeF _lastSize;
        public virtual void Draw(Graphics g)
        {
            if (string.IsNullOrEmpty(Text) || Font == null) return;

            if (_lastText != Text)
            {
                _lastText = Text;
                _lastSize = _lastText.MeasureString(Font);
            }
            IntPtr hdc = g.GetHdc();
            int w = (int)Math.Ceiling(_lastSize.Width) << 1;
            int h = (int)Math.Ceiling(_lastSize.Height) << 1;
            int x = X - (w >> 1);
            int y = Y;
            GDI32.DrawImageTransparenc(hdc, _image.BitmapPtr, x, y, _image.Width, _image.Height, w, h);
            g.ReleaseHdc(hdc);

            g.DrawString(_lastText, Font, Brushes.White, x + 1, y + 1);
        }
        public virtual void Dispose()
        {
            _image?.Dispose();
        }
    }
}
